iT邦幫忙

2022 iThome 鐵人賽

DAY 4
0
自我挑戰組

30天Java由淺入深系列 第 4

Day 4 : Java Basic Syntax

  • 分享至 

  • xImage
  •  

#Intro

At the beginning, we will introduce the basic syntax of Java, including variables, loops, functions, etc. We will enter object-oriented teaching almost in the last two weeks, introducing some class objects and related advanced concepts. This is done in the hope that users with no programming experience at all can learn a programming language from the basic to the advanced and use it as a tool in the future.
(This series of tutorials references W3Schools.)https://ithelp.ithome.com.tw/upload/images/20220919/20151216O1HT0GgLqW.png


In Java, every program starts with a class name, and every executable instruction must be placed inside that class. Additionally, the name of the .java file must match the class name.

  • One more thing to note: Java is a case-sensitive programming language, meaning that uppercase and lowercase letters are treated differently. This is something you need to pay particular attention to!
//Filename : Main.java

public class Main{                              
	public static void main(String[] args){       
		System.out.println("Hello IThome");         
	}
}

#Code Parsing

  • public class Main{}
    Here public means and can be accessed by other classes at the same time, as described in more detail below.
    → Here public means and can be accessed by other classes at the same time, as described in more detail below.

  • public static void main(String[ ] args){}
    main can be regarded as the main program (which must exist for every program), and all instructions to be executed will be placed in it.
    →Other keywords such as static and void will be introduced later, so for now, just remember the function of the program.

  • System.out.println(”Hello IThome”) ;
    This is to inform the system to print the content in ( ), and output it to the screen using the println() command
    →This line is the content to be executed, and it is also placed in main (remember to place the content to be output in “ ”)
    →Because the end of a line, we must inform the computer that this line has ended, so we add ; at the end.


// This is a single-line comment
/* This is a multi-line comment*/

In any language, we will face projects with complex code that is divided into different files. Annotation is therefore an important step at this time, not only to help you quickly understand the content, but also to help others understand your code.​

There are two ways to comment in Java:

  1. // This is a single-line comment, and its scope is limited to the same line
  2. /* */ This is a multi-line comment, and its scope can cover multiple lines

#Output Example

We can also perform mathematical operations using println().

System.out.println(126 + 919);          //Outputs : 1045
System.out.println(126 * 919);          //Outputs : 115794

In addition, there is another keyword, print(). The big difference is that the output will not wrap.
→ less ln, meaning new line.

System.out.print("Hello ");
System.out.print("World");        //Outputs : Hello World

System.out.println("Hello ");
System.out.println("World");      /* Outputs : Hello
                                               World  */

If there are any errors in the above content, please do not hesitate to let me know, thank you!!!!
/images/emoticon/emoticon41.gif


上一篇
Day 3 : Notion
下一篇
Day 5 : Variables( 1 )
系列文
30天Java由淺入深30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言